package com.dayuanit.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil {

	public static void sendMail(String to, String code) throws AddressException, MessagingException {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", "smtp.126.com");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("cookiegkl@126.com", "gzh19911212");
			}
		});
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress("cookiegkl@126.com"));
		message.setRecipient(RecipientType.TO, new InternetAddress(to));
		message.setSubject("来自http://http://www.dayuanmall.com:8080/的激活邮件");
		message.setContent("<h1>来自http://http://www.dayuanmall.com:8080/的激活邮件,激活请点击以下链接：</h1><h3><a href='http://http://www.dayuanmall.com:8080/user/activation.do?code=" + code + "'>http://http://www.dayuanmall.com:8080/user/activation.do?code=" + code + "</a></h3>", "text/html;charset=UTF-8");
		Transport.send(message);
	}
	
	public static void forget(String email, String username) throws AddressException, MessagingException {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", "smtp.126.com");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("cookiegkl@126.com", "gzh19911212");
			}
		});
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress("cookiegkl@126.com"));
		message.setRecipient(RecipientType.TO, new InternetAddress(email));
		message.setSubject("来自http://http://www.dayuanmall.com:8080/的重置密码邮件");
		message.setContent("<h1>来自http://http://www.dayuanmall.com:8080/的重置密码邮件,重置请点击以下链接：</h1><h3><a href='http://http://www.dayuanmall.com:8080/user/toModifyPassword.do?userName=" + username + "&mail="+email+"'>http://http://www.dayuanmall.com:8080/user/toModifyPassword.do?userName=" + username + "&mail="+email+"</a></h3>", "text/html;charset=UTF-8");
		Transport.send(message);
	}
}
